home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / TE32K.sit / TE32K Demo Folder / TE32K Source ƒ / main.c next >
C/C++ Source or Header  |  1993-01-02  |  21KB  |  1,058 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "TE32K.h"
  4.  
  5.  
  6. #define EQSTR    0
  7.  
  8.  
  9. #define    yesNoCancelDLOG    132
  10. #define    errorDLOG        256
  11.  
  12. #define    numMenus    4
  13.  
  14. #define    appleMenu    0
  15. #define    fileMenu    1
  16. #define    editMenu    2
  17. #define fontMenu    3
  18.  
  19. #define    appleID        128
  20. #define    fileID        129
  21. #define    editID        130
  22. #define fontID        131
  23.  
  24. #define    newCommand        1
  25. #define    openCommand        2
  26. #define insertCommand    3
  27. #define    closeCommand    4
  28. #define    saveCommand        5
  29. #define    saveAsCommand    6
  30. #define    quitCommand        8
  31.  
  32. #define    undoCommand        1
  33. #define    cutCommand        3
  34. #define    copyCommand        4
  35. #define pasteCommand    5
  36. #define    clearCommand    6
  37. #define    selectCommand    7
  38. #define    wrapCommand        9
  39.  
  40.  
  41.  
  42. char            alive;
  43. MenuHandle        myMenus[numMenus];
  44. char            tempString[256];
  45. WindowPtr        theWPtr = 0L;
  46. TE32KHandle        theTEH32K = 0L;
  47. ControlHandle    theVScroll = 0L, theHScroll = 0L;
  48. Cursor            editCursor,waitCursor;
  49. SFReply            mySFReply;
  50. char            changed;
  51. int                defaultFont,defaultFontSize;
  52.  
  53.  
  54. RestartProc()
  55. {
  56.     ExitToShell();
  57. }
  58.  
  59.  
  60.  
  61. main()
  62. {
  63. EventRecord        theEvent;
  64. WindowPtr        whichWindow;
  65. unsigned char    theChar;
  66.  
  67.     InitGraf(&thePort);
  68.     InitFonts();
  69.     FlushEvents(everyEvent,0);
  70.     InitWindows();
  71.     InitMenus();
  72.     TEInit();
  73.     InitDialogs(RestartProc);
  74.     InitCursor();
  75.     
  76.     TE32KInit();
  77.     
  78.     SetUpCursors();
  79.     
  80.     SetUpMenus();
  81.     
  82.     defaultFont = monaco;
  83.     defaultFontSize = 9;
  84.     MaintainFontMenu();
  85.     
  86.     FlushEvents(everyEvent,0);
  87.     
  88.     alive = TRUE;
  89.     
  90.     while (alive)
  91.     {
  92.         SystemTask();
  93.         MaintainCursor();
  94.         MaintainMenus();
  95.         DoIdle();
  96.         
  97.         if (GetNextEvent(everyEvent,&theEvent))
  98.         {
  99.             switch (theEvent.what)
  100.             {
  101.                 case mouseDown:
  102.                     DoMouseDown(&theEvent);
  103.                     break;
  104.                 
  105.                 case keyDown:
  106.                 case autoKey: 
  107.                     
  108.                     theChar = theEvent.message & charCodeMask;
  109.                     
  110.                     if (theEvent.modifiers & cmdKey)
  111.                         DoCommand(MenuKey((unsigned char) theChar));
  112.                     
  113.                     else
  114.                         DoKey((unsigned char) theChar);
  115.                     
  116.                     break;
  117.                     
  118.                 case activateEvt:
  119.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  120.                         DoActivateDeactivate((WindowPtr) theEvent.message,(char) (theEvent.modifiers & activeFlag));
  121.                     
  122.                     break;
  123.                     
  124.                 case updateEvt: 
  125.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  126.                         UpdateWindow((WindowPtr) theEvent.message);
  127.                     
  128.                     break;
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. pascal void HiliteDefaultButton(theDPtr, whichItem)
  139. DialogPtr theDPtr; int whichItem;
  140. {
  141. int            type;
  142. Handle        theItem;
  143. Rect        theRect;
  144.  
  145.     GetDItem(theDPtr, ((DialogPeek) theDPtr)->aDefItem, &type, &theItem, &theRect);
  146.     PenNormal();
  147.     PenSize(3,3);
  148.     InsetRect(&theRect,-4,-4);
  149.     FrameRoundRect(&theRect,16,16);
  150.     PenSize(1,1);
  151. }
  152.  
  153.  
  154.  
  155. ErrorAlert(p0)
  156. char    *p0;
  157. {
  158. GrafPtr            oldPort;
  159. DialogPtr        theDPtr,tempDPtr;
  160. int                itemHit, type;
  161. Handle            theItem;
  162. Rect            theRect;
  163. EventRecord        theEvent;
  164.  
  165.     GetPort(&oldPort);
  166.     
  167.     InitCursor();
  168.     
  169.     if (!(theDPtr = GetNewDialog(errorDLOG, NULL,(WindowPtr) -1L)))
  170.     {
  171.         SysBeep(1);
  172.         ExitToShell();
  173.     }
  174.     
  175.     SetPort(theDPtr);
  176.     
  177.     CenterWindow(theDPtr);
  178.     ShowWindow(theDPtr);
  179.     
  180.     ((DialogPeek) theDPtr)->aDefItem = 1;
  181.     
  182.     GetDItem(theDPtr, 3, &type, &theItem, &theRect);
  183.     SetDItem(theDPtr, 3, type, HiliteDefaultButton, &theRect);
  184.     
  185.     GetDItem(theDPtr, 2, &type, &theItem, &theRect);
  186.     CtoPstr(p0);
  187.     SetIText(theItem, p0);
  188.     PtoCstr(p0);
  189.     
  190.     while (!GetNextEvent(updateMask,&theEvent));
  191.     
  192.     if (theEvent.message == (long) theDPtr)
  193.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  194.     
  195.     SysBeep(1);
  196.     
  197.     do 
  198.     {
  199.         itemHit = 0;
  200.         
  201.         while (!GetNextEvent(everyEvent,&theEvent));
  202.         
  203.         if (theEvent.what==keyDown || theEvent.what==autoKey)
  204.         {
  205.             if ((theEvent.message & charCodeMask)=='\r' || (theEvent.message & charCodeMask)==0x03)
  206.                 itemHit = ((DialogPeek) theDPtr)->aDefItem;
  207.             else
  208.                 SysBeep(1);
  209.         }
  210.         
  211.         else
  212.         {
  213.             tempDPtr = (DialogPtr) 0L;
  214.             
  215.             if (!IsDialogEvent(&theEvent) || !DialogSelect(&theEvent,&tempDPtr,&itemHit) || tempDPtr!=theDPtr)
  216.                 itemHit = 0;
  217.         }
  218.         
  219.     } while (itemHit!=1);
  220.     
  221.     DisposDialog(theDPtr);
  222.     SetPort(oldPort);
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229. SetUpMenus()
  230. {
  231. int            i,numDAs;
  232.  
  233.     myMenus[appleMenu] = GetMenu(appleID);
  234.     AddResMenu(myMenus[appleMenu],'DRVR');
  235.     myMenus[fileMenu] = GetMenu(fileID);
  236.     myMenus[editMenu] = GetMenu(editID);
  237.     myMenus[fontMenu] = GetMenu(fontID);
  238.     InsertResMenu(myMenus[fontMenu],'FONT',0);
  239.     
  240.     InsertMenu(myMenus[appleMenu],0);
  241.     InsertMenu(myMenus[fileMenu],0);
  242.     InsertMenu(myMenus[editMenu],0);
  243.     InsertMenu(myMenus[fontMenu],0);
  244.     
  245.     DrawMenuBar();
  246.     
  247.     numDAs = CountMItems(myMenus[appleMenu]);
  248.     
  249.     for (i=1;i<=numDAs;i++)
  250.         EnableItem(myMenus[appleMenu],i);
  251. }
  252.  
  253.  
  254.  
  255. MaintainMenus()
  256. {
  257. int        i,numItems;
  258.  
  259.     if (theWPtr && FrontWindow() == theWPtr)
  260.     {
  261.         DisableItem(myMenus[fileMenu],newCommand);
  262.         DisableItem(myMenus[fileMenu],openCommand);
  263.         EnableItem(myMenus[fileMenu],insertCommand);
  264.         EnableItem(myMenus[fileMenu],closeCommand);
  265.         
  266.         if (changed)
  267.             EnableItem(myMenus[fileMenu],saveCommand);
  268.         else
  269.             DisableItem(myMenus[fileMenu],saveCommand);
  270.         
  271.         EnableItem(myMenus[fileMenu],saveAsCommand);
  272.         
  273.         
  274.         DisableItem(myMenus[editMenu],undoCommand);
  275.         
  276.         if (theTEH32K)
  277.         {
  278.             if ((**theTEH32K).selStart == (**theTEH32K).selEnd)
  279.             {
  280.                 DisableItem(myMenus[editMenu],cutCommand);
  281.                 DisableItem(myMenus[editMenu],copyCommand);
  282.                 DisableItem(myMenus[editMenu],clearCommand);
  283.             }
  284.             
  285.             else
  286.             {
  287.                 EnableItem(myMenus[editMenu],cutCommand);
  288.                 EnableItem(myMenus[editMenu],copyCommand);
  289.                 EnableItem(myMenus[editMenu],clearCommand);
  290.             }
  291.             
  292.             if (TE32KGetScrapLen() > 0L)
  293.                 EnableItem(myMenus[editMenu],pasteCommand);
  294.             else
  295.                 DisableItem(myMenus[editMenu],pasteCommand);
  296.             
  297.             EnableItem(myMenus[editMenu],selectCommand);
  298.             EnableItem(myMenus[editMenu],wrapCommand);
  299.             
  300.             if (theTEH32K && !(**theTEH32K).crOnly)
  301.             {
  302.                 CheckItem(myMenus[editMenu],wrapCommand,TRUE);
  303.                 SetItemMark(myMenus[editMenu],wrapCommand,checkMark);
  304.             }
  305.                 
  306.             else
  307.                 CheckItem(myMenus[editMenu],wrapCommand,FALSE);
  308.         }
  309.         
  310.         
  311.         numItems = CountMItems(myMenus[fontMenu]);
  312.         
  313.         for (i=1;i<=numItems;i++)
  314.             EnableItem(myMenus[fontMenu],i);
  315.         
  316.         MaintainFontMenu();
  317.     }
  318.     
  319.     else
  320.     {
  321.         EnableItem(myMenus[fileMenu],newCommand);
  322.         EnableItem(myMenus[fileMenu],openCommand);
  323.         DisableItem(myMenus[fileMenu],insertCommand);
  324.         DisableItem(myMenus[fileMenu],closeCommand);
  325.         DisableItem(myMenus[fileMenu],saveCommand);
  326.         DisableItem(myMenus[fileMenu],saveAsCommand);
  327.         
  328.         
  329.         EnableItem(myMenus[editMenu],undoCommand);
  330.         EnableItem(myMenus[editMenu],cutCommand);
  331.         EnableItem(myMenus[editMenu],copyCommand);
  332.         EnableItem(myMenus[editMenu],pasteCommand);
  333.         EnableItem(myMenus[editMenu],clearCommand);
  334.         DisableItem(myMenus[editMenu],selectCommand);
  335.         DisableItem(myMenus[editMenu],wrapCommand);
  336.         CheckItem(myMenus[editMenu],wrapCommand,FALSE);
  337.         
  338.         numItems = CountMItems(myMenus[fontMenu]);
  339.         
  340.         for (i=1;i<=numItems;i++)
  341.         {
  342.             DisableItem(myMenus[fontMenu],i);
  343.             CheckItem(myMenus[fontMenu],i,FALSE);
  344.         }
  345.     }
  346.  
  347. }
  348.  
  349.  
  350. DoCommand(mResult)
  351. long mResult;
  352. {
  353. int            theItem,numItems,userChoice;
  354. WindowPeek     wPtr;
  355. GrafPtr        oldPort;
  356. SFReply        oldSFReply;
  357. Rect        tempRect;
  358.  
  359.  
  360.     theItem = LoWord(mResult);
  361.     
  362.     switch (HiWord(mResult)) 
  363.     {
  364.         case appleID:
  365.             if (theItem == 1)
  366.                 DoAboutBox(0);
  367.             
  368.             else
  369.             {
  370.                 GetPort(&oldPort);
  371.                 GetItem(myMenus[appleMenu], theItem, &tempString);
  372.                 OpenDeskAcc(&tempString);
  373.                 SetPort(oldPort);
  374.             }
  375.             
  376.             break;
  377.             
  378.         case fileID:
  379.             switch (theItem)
  380.             {
  381.                 case newCommand:
  382.                     if (!theTEH32K)
  383.                         DoShowWindow();
  384.                     else
  385.                         SysBeep(1);
  386.                     
  387.                     break;
  388.                 
  389.                 case openCommand:
  390.                     if (theTEH32K && DoCloseWindow(theWPtr))
  391.                     {
  392.                         DoShowWindow();
  393.                         
  394.                         if (OpenTextFile(theTEH32K,&mySFReply))
  395.                         {
  396.                             SetWTitle(theWPtr,mySFReply.fName);
  397.                             AdjustScrollBar();
  398.                             changed = FALSE;
  399.                         }
  400.                     }
  401.                     
  402.                     else if (!theTEH32K)
  403.                     {
  404.                         DoShowWindow();
  405.                         
  406.                         if (OpenTextFile(theTEH32K,&mySFReply))
  407.                         {
  408.                             SetWTitle(theWPtr,mySFReply.fName);
  409.                             AdjustScrollBar();
  410.                             changed = FALSE;
  411.                         }
  412.                     }
  413.                     
  414.                     break;
  415.                 
  416.                 case insertCommand:
  417.                     if (theTEH32K)
  418.                     {
  419.                         changed = InsertTextFile(theTEH32K);
  420.                         
  421.                         AdjustScrollBar();
  422.                     }
  423.                     
  424.                     break;
  425.                     
  426.                 case closeCommand:
  427.                     if (theTEH32K)
  428.                         DoCloseWindow(theWPtr);
  429.                     else
  430.                         SysBeep(1);
  431.                         
  432.                     break;
  433.                 
  434.                 case saveCommand:
  435.                     if (theTEH32K)
  436.                     {
  437.                         if (SaveTextFile(theTEH32K,&mySFReply))
  438.                         {
  439.                             SetWTitle(theWPtr,mySFReply.fName);
  440.                             changed = FALSE;
  441.                         }
  442.                     }
  443.                         
  444.                     else
  445.                         SysBeep(1);
  446.                     
  447.                     break;
  448.                 
  449.                 case saveAsCommand:
  450.                     if (theTEH32K)
  451.                     {
  452.                         oldSFReply = mySFReply;
  453.                         mySFReply.good = FALSE;
  454.                         
  455.                         if (SaveTextFile(theTEH32K,&mySFReply))
  456.                         {
  457.                             changed = FALSE;
  458.                             SetWTitle(theWPtr,mySFReply.fName);
  459.                         }
  460.                         else
  461.                             mySFReply = oldSFReply;
  462.                     }
  463.                         
  464.                     else
  465.                         SysBeep(1);
  466.                     
  467.                     break;
  468.                 
  469.                 case quitCommand:
  470.                     if (theTEH32K)
  471.                     {
  472.                         if (DoCloseWindow(theWPtr))
  473.                             alive = FALSE;
  474.                     }
  475.                     else
  476.                         alive = FALSE;
  477.                     
  478.                     break;
  479.             }
  480.             
  481.             break;
  482.         
  483.         case editID:
  484.             switch(theItem)
  485.             {
  486.                 case cutCommand:
  487.                     if (!DoCut())
  488.                         SystemEdit(theItem - 1);
  489.                     break;
  490.                 
  491.                 case copyCommand:
  492.                     if (!DoCopy())
  493.                         SystemEdit(theItem - 1);
  494.                     break;
  495.                 
  496.                 case pasteCommand:
  497.                     if (!DoPaste())
  498.                         SystemEdit(theItem - 1);
  499.                     
  500.                     break;
  501.                 
  502.                 case clearCommand:
  503.                     if (!DoClear())
  504.                         SystemEdit(theItem - 1);
  505.                     
  506.                     break;
  507.                 
  508.                 case selectCommand:
  509.                     if (!DoSelectAll())
  510.                         SystemEdit(theItem - 1);
  511.                     
  512.                     break;
  513.                 
  514.                 case wrapCommand:
  515.                     if (theWPtr && theWPtr == FrontWindow() && theTEH32K)
  516.                     {
  517.                         (**theTEH32K).crOnly = !(**theTEH32K).crOnly;
  518.                         
  519.                         TE32KCalText(theTEH32K);
  520.                         
  521.                         tempRect = theWPtr->portRect;
  522.                         GetPort(&oldPort);
  523.                         SetPort(theWPtr);
  524.                         InvalRect(&tempRect);
  525.                         SetPort(oldPort);
  526.                         
  527.                         AdjustForResizedWindow();
  528.                     }
  529.                     
  530.                     break;
  531.                 
  532.                 
  533.                 default:
  534.                     SystemEdit(theItem - 1);
  535.                     break;
  536.             }
  537.             
  538.             break;
  539.             
  540.         case fontID:
  541.             numItems = CountMItems(myMenus[fontMenu]);
  542.             
  543.             if (theItem <= numItems-7)
  544.             {
  545.                 GetItem(myMenus[fontMenu], theItem, &tempString);
  546.                 PtoCstr(tempString);
  547.                 DoFontSize(tempString,-1);
  548.             }
  549.             
  550.             else
  551.             {
  552.                 GetItem(myMenus[fontMenu], theItem, &tempString);
  553.                 PtoCstr(tempString);
  554.                 
  555.                 if (sscanf(tempString,"%d",&numItems)==1)
  556.                 {
  557.                     tempString[0] = 0;
  558.                     DoFontSize(tempString,numItems);
  559.                 }
  560.             }
  561.             
  562.             break;
  563.     }
  564.     
  565.     HiliteMenu(0);
  566. }
  567.  
  568.  
  569.  
  570.  
  571. OpenTextFile(theTEH,mySFReply)
  572. TE32KHandle    theTEH;SFReply    *mySFReply;
  573. {
  574. SFTypeList    mySFTypes;
  575. Point        theTopLeft;
  576. int            errCode,fRefNum;
  577. long        byteCount;
  578. Ptr            theText;
  579.  
  580.  
  581.     if (theTEH)
  582.     {
  583.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 348)/2;;
  584.         theTopLeft.v = screenBits.bounds.top + 50;
  585.         
  586.         mySFTypes[0] = 'TEXT';
  587.         
  588.         SFGetFile(theTopLeft,"\Select Text File To Open:",0L,1,&mySFTypes,0L,mySFReply);
  589.         
  590.         if (mySFReply->good)
  591.         {
  592.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  593.             {
  594.                 mySFReply->good = FALSE;
  595.                 PtoCstr((char *) mySFReply->fName);
  596.                 sprintf(tempString,"OpenTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  597.                 ErrorAlert(tempString);
  598.                 return(FALSE);
  599.             }
  600.             
  601.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  602.             {
  603.                 mySFReply->good = FALSE;
  604.                 FSClose(fRefNum);
  605.                 PtoCstr((char *) mySFReply->fName);
  606.                 sprintf(tempString,"OpenTextFile: GetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  607.                 ErrorAlert(tempString);
  608.                 return(FALSE);
  609.             }
  610.             
  611.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  612.             {
  613.                 mySFReply->good = FALSE;
  614.                 FSClose(fRefNum);
  615.                 PtoCstr((char *) mySFReply->fName);
  616.                 sprintf(tempString,"OpenTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  617.                 ErrorAlert(tempString);
  618.                 return(FALSE);
  619.             }
  620.             
  621.             theText = NewPtr(byteCount);
  622.             
  623.             if (StripAddress(theText)==0L || MemError())
  624.             {
  625.                 mySFReply->good = FALSE;
  626.                 FSClose(fRefNum);
  627.                 sprintf(tempString,"OpenTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  628.                 ErrorAlert(tempString);
  629.                 return(FALSE);
  630.             }
  631.             
  632.             SetCursor(&waitCursor);
  633.             
  634.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  635.             {
  636.                 InitCursor();
  637.                 mySFReply->good = FALSE;
  638.                 DisposPtr(theText);
  639.                 FSClose(fRefNum);
  640.                 PtoCstr((char *) mySFReply->fName);
  641.                 sprintf(tempString,"OpenTextFile: FSRead('%s') Error %d",mySFReply->fName,errCode);
  642.                 ErrorAlert(tempString);
  643.                 return(FALSE);
  644.             }
  645.             
  646.             if ((errCode = FSClose(fRefNum))!=noErr)
  647.             {
  648.                 InitCursor();
  649.                 mySFReply->good = FALSE;
  650.                 DisposPtr(theText);
  651.                 PtoCstr((char *) mySFReply->fName);
  652.                 sprintf(tempString,"OpenTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  653.                 ErrorAlert(tempString);
  654.                 return(FALSE);
  655.             }
  656.             
  657.             TE32KSetText(theText,byteCount,theTEH);
  658.             
  659.             DisposPtr(theText);
  660.             
  661.             InitCursor();
  662.             
  663.             return(TRUE);
  664.         }
  665.         
  666.         else
  667.             return(FALSE);
  668.     }
  669.     
  670.     else
  671.         return(FALSE);
  672. }
  673.  
  674.  
  675.  
  676. SaveTextFile(theTEH,mySFReply)
  677. TE32KHandle    theTEH;SFReply    *mySFReply;
  678. {
  679. Point        theTopLeft;
  680. int            errCode,fRefNum;
  681. long        byteCount;
  682. Handle        theText;
  683.  
  684.     if (theTEH)
  685.     {
  686.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 304)/2;;
  687.         theTopLeft.v = screenBits.bounds.top + 50;
  688.         
  689.         if (!(mySFReply->good))
  690.             SFPutFile(theTopLeft,"\pSave Text File As:","\p",0L,mySFReply);
  691.         
  692.         if (mySFReply->good)
  693.         {
  694.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  695.             {
  696.                 if (errCode==fnfErr)
  697.                 {
  698.                     if ((errCode = Create(mySFReply->fName,mySFReply->vRefNum,'????','TEXT'))!=noErr)
  699.                     {
  700.                         mySFReply->good = FALSE;
  701.                         PtoCstr((char *) mySFReply->fName);
  702.                         sprintf(tempString,"SaveTextFile: Create('%s') Error %d",mySFReply->fName,errCode);
  703.                         ErrorAlert(tempString);
  704.                         return(FALSE);
  705.                     }
  706.                     
  707.                     errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum);
  708.                 }
  709.                 
  710.                 if (errCode != noErr)
  711.                 {
  712.                     mySFReply->good = FALSE;
  713.                     PtoCstr((char *) mySFReply->fName);
  714.                     sprintf(tempString,"SaveTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  715.                     ErrorAlert(tempString);
  716.                     return(FALSE);
  717.                 }
  718.             }
  719.             
  720.             if ((errCode = SetEOF(fRefNum,0L))!=noErr)
  721.             {
  722.                 mySFReply->good = FALSE;
  723.                 FSClose(fRefNum);
  724.                 PtoCstr((char *) mySFReply->fName);
  725.                 sprintf(tempString,"SaveTextFile: SetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  726.                 ErrorAlert(tempString);
  727.                 return(FALSE);
  728.             }
  729.             
  730.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  731.             {
  732.                 mySFReply->good = FALSE;
  733.                 FSClose(fRefNum);
  734.                 PtoCstr((char *) mySFReply->fName);
  735.                 sprintf(tempString,"SaveTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  736.                 ErrorAlert(tempString);
  737.                 return(FALSE);
  738.             }
  739.             
  740.             theText = (**theTEH).hText;
  741.             byteCount = (**theTEH).teLength;
  742.             
  743.             HLock(theText);
  744.             
  745.             SetCursor(&waitCursor);
  746.             
  747.             if ((errCode = FSWrite(fRefNum,&byteCount,*theText))!=noErr)
  748.             {
  749.                 InitCursor();
  750.                 mySFReply->good = FALSE;
  751.                 HUnlock(theText);
  752.                 FSClose(fRefNum);
  753.                 PtoCstr((char *) mySFReply->fName);
  754.                 sprintf(tempString,"SaveTextFile: FSWrite('%s') Error %d",mySFReply->fName,errCode);
  755.                 ErrorAlert(tempString);
  756.                 return(FALSE);
  757.             }
  758.             
  759.             HUnlock(theText);
  760.             
  761.             if ((errCode = FSClose(fRefNum))!=noErr)
  762.             {
  763.                 InitCursor();
  764.                 mySFReply->good = FALSE;
  765.                 PtoCstr((char *) mySFReply->fName);
  766.                 sprintf(tempString,"SaveTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  767.                 ErrorAlert(tempString);
  768.                 return(FALSE);
  769.             }
  770.             
  771.             FlushVol("\p",mySFReply->vRefNum);
  772.             
  773.             InitCursor();
  774.             
  775.             return(TRUE);
  776.         }
  777.         
  778.         else
  779.             return(FALSE);
  780.     }
  781.     
  782.     else
  783.         return(FALSE);
  784. }
  785.  
  786.  
  787.  
  788.  
  789. YesNoCancel(thePrompt,theDefault)
  790. char    *thePrompt;int    theDefault;
  791. {
  792. DialogPtr        theDPtr,tempDPtr;
  793. int                itemHit, type;
  794. Handle            theItem;
  795. Rect            tempRect;
  796. GrafPtr            oldPort;
  797. char            tempString[256];
  798. EventRecord        theEvent;
  799.  
  800.     GetPort(&oldPort);
  801.  
  802.     theDPtr = GetNewDialog(yesNoCancelDLOG, 0L,(WindowPtr) -1L);
  803.     
  804.     CenterWindow(theDPtr);
  805.     ShowWindow(theDPtr);
  806.     
  807.     SetPort(theDPtr);
  808.     
  809.     ((DialogPeek) theDPtr)->aDefItem = theDefault;
  810.     
  811.     GetDItem(theDPtr, 4, &type, &theItem, &tempRect);
  812.     SetDItem(theDPtr, 4, type, HiliteDefaultButton, &tempRect);
  813.     
  814.     strcpy(tempString,thePrompt);
  815.     CtoPstr(tempString);
  816.     GetDItem(theDPtr, 5, &type, &theItem, &tempRect);
  817.     SetIText(theItem, tempString);
  818.     
  819.     if (GetNextEvent(updateMask,&theEvent) && theEvent.message == (long) theDPtr)
  820.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  821.     
  822.     SysBeep(1);
  823.     
  824.     do 
  825.     {
  826.         ModalDialog(0L,&itemHit);
  827.         
  828.     } while (!(itemHit>=1 && itemHit<=3));
  829.     
  830.     DisposDialog(theDPtr);
  831.     SetPort(oldPort);
  832.     
  833.     return(itemHit);
  834. }
  835.  
  836.  
  837.  
  838.  
  839. CenterWindow(wPtr)
  840. WindowPtr    wPtr;
  841. {
  842. int        screenWidth,screenHeight,windowWidth,windowHeight,left,top;
  843.  
  844.     if (wPtr == 0L)
  845.         return;
  846.     
  847.     screenWidth = screenBits.bounds.right - screenBits.bounds.left;
  848.     screenHeight = screenBits.bounds.bottom - screenBits.bounds.top - 20;
  849.     windowWidth = wPtr->portRect.right - wPtr->portRect.left;
  850.     windowHeight = wPtr->portRect.bottom - wPtr->portRect.top;
  851.     
  852.     left = screenBits.bounds.left + (screenWidth - windowWidth)/2;
  853.     top = screenBits.bounds.top + 20 + (screenHeight - windowHeight)/2;
  854.     
  855.     if (left < 0)
  856.         left = 0;
  857.     
  858.     if (top < 20)
  859.         top = 20;
  860.     
  861.     MoveWindow(wPtr,left,top,FALSE);
  862. }
  863.  
  864.  
  865.  
  866.  
  867. InsertTextFile(theTEH)
  868. TE32KHandle    theTEH;
  869. {
  870. SFReply        mySFReply;
  871. SFTypeList    mySFTypes;
  872. Point        theTopLeft;
  873. int            errCode,fRefNum;
  874. long        byteCount;
  875. Ptr            theText;
  876. char        tempString[256];
  877.  
  878.     if (theTEH)
  879.     {
  880.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 348)/2;;
  881.         theTopLeft.v = screenBits.bounds.top + 50;
  882.         
  883.         mySFTypes[0] = 'TEXT';
  884.         
  885.         SFGetFile(theTopLeft,"\Select Text File To Insert:",0L,1,&mySFTypes,0L,&mySFReply);
  886.         
  887.         if (mySFReply.good)
  888.         {
  889.             if ((errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum))!=noErr)
  890.             {
  891.                 PtoCstr((char *) mySFReply.fName);
  892.                 sprintf(tempString,"InsertTextFile: FSOpen('%s') Error %d",mySFReply.fName,errCode);
  893.                 ErrorAlert(tempString);
  894.                 return(TRUE);
  895.             }
  896.             
  897.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  898.             {
  899.                 FSClose(fRefNum);
  900.                 PtoCstr((char *) mySFReply.fName);
  901.                 sprintf(tempString,"InsertTextFile: GetEOF('%s',0) Error %d",mySFReply.fName,errCode);
  902.                 ErrorAlert(tempString);
  903.                 return(TRUE);
  904.             }
  905.             
  906.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  907.             {
  908.                 FSClose(fRefNum);
  909.                 PtoCstr((char *) mySFReply.fName);
  910.                 sprintf(tempString,"InsertTextFile: SetFPos('%s',0) Error %d",mySFReply.fName,errCode);
  911.                 ErrorAlert(tempString);
  912.                 return(TRUE);
  913.             }
  914.             
  915.             theText = NewPtr(byteCount);
  916.             
  917.             if (StripAddress(theText)==0L || MemError())
  918.             {
  919.                 FSClose(fRefNum);
  920.                 sprintf(tempString,"InsertTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  921.                 ErrorAlert(tempString);
  922.                 return(TRUE);
  923.             }
  924.             
  925.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  926.             {
  927.                 DisposPtr(theText);
  928.                 FSClose(fRefNum);
  929.                 PtoCstr((char *) mySFReply.fName);
  930.                 sprintf(tempString,"InsertTextFile: FSRead('%s') Error %d",mySFReply.fName,errCode);
  931.                 ErrorAlert(tempString);
  932.                 return(TRUE);
  933.             }
  934.             
  935.             if ((errCode = FSClose(fRefNum))!=noErr)
  936.             {
  937.                 DisposPtr(theText);
  938.                 PtoCstr((char *) mySFReply.fName);
  939.                 sprintf(tempString,"InsertTextFile: FSClose('%s',0) Error %d",mySFReply.fName,errCode);
  940.                 ErrorAlert(tempString);
  941.                 return(TRUE);
  942.             }
  943.             
  944.             SetCursor(&waitCursor);
  945.             
  946.             TE32KInsert(theText,byteCount,theTEH);
  947.             
  948.             InitCursor();
  949.             
  950.             DisposPtr(theText);
  951.         }
  952.         
  953.         return(TRUE);
  954.     }
  955.     
  956.     else
  957.         return(FALSE);
  958. }
  959.  
  960.  
  961.  
  962.  
  963. DoFontSize(theFontName,theFontSize)
  964. char *theFontName;int    theFontSize;
  965. {
  966. WindowPtr    theWPtr;
  967. int            theFontNum;
  968. Rect        tempRect;
  969. GrafPtr        oldPort;
  970.     
  971.     if (theTEH32K)
  972.     {
  973.         if (theFontName[0])
  974.         {
  975.             CtoPstr(theFontName);
  976.             GetFNum(theFontName,&theFontNum);
  977.         }
  978.         else
  979.             theFontNum = -1;
  980.         
  981.         
  982.         if (theFontNum < 0)
  983.             theFontNum = defaultFont;
  984.         
  985.         if (theFontSize <= 0)
  986.             theFontSize = defaultFontSize;
  987.             
  988.         defaultFont = theFontNum;
  989.         defaultFontSize = theFontSize;
  990.         
  991.         TE32KSetFontStuff(theFontNum,0,srcCopy,theFontSize,theTEH32K);
  992.         
  993.         AdjustForResizedWindow();
  994.         
  995.         GetPort(&oldPort);
  996.         SetPort((**theTEH32K).inPort);
  997.         tempRect = (*(**theTEH32K).inPort).portRect;
  998.         InvalRect(&tempRect);
  999.         SetPort(oldPort);
  1000.         
  1001.         MaintainFontMenu();
  1002.     }
  1003. }
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009. MaintainFontMenu()
  1010. {
  1011. int        i,numItems,tempSize;
  1012. char    theFontString[128];
  1013. char    tempString[256];
  1014.  
  1015.     numItems = CountMItems(myMenus[fontMenu]);
  1016.     
  1017.     GetFontName(defaultFont,theFontString);
  1018.     PtoCstr(theFontString);
  1019.     
  1020.     for (i=1;i<=numItems-7;i++)
  1021.     {
  1022.         GetItem(myMenus[fontMenu],i,&tempString);
  1023.         PtoCstr(tempString);
  1024.         
  1025.         if (strcmp(tempString,theFontString) == EQSTR)
  1026.         {
  1027.             CheckItem(myMenus[fontMenu],i,TRUE);
  1028.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1029.         }
  1030.         else
  1031.             CheckItem(myMenus[fontMenu],i,FALSE);
  1032.     }
  1033.     
  1034.     
  1035.     sprintf(theFontString,"%d",defaultFontSize);
  1036.     
  1037.     for (i=numItems-6;i<=numItems;i++)
  1038.     {
  1039.         GetItem(myMenus[fontMenu],i,&tempString);
  1040.         PtoCstr(tempString);
  1041.         
  1042.         if (sscanf(tempString,"%d",&tempSize) != 1)
  1043.             tempSize = -1;
  1044.         
  1045.         if (strcmp(tempString,theFontString) == EQSTR)
  1046.         {
  1047.             CheckItem(myMenus[fontMenu],i,TRUE);
  1048.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1049.         }
  1050.         else
  1051.             CheckItem(myMenus[fontMenu],i,FALSE);
  1052.         
  1053.         if (RealFont(defaultFont,tempSize))
  1054.             SetItemStyle(myMenus[fontMenu],i,outline);
  1055.         else
  1056.             SetItemStyle(myMenus[fontMenu],i,0);
  1057.     }
  1058. }